package thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//CountDonwnLatch使用案例
public class Demo23 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch count = new CountDownLatch(10);

        ExecutorService executorService = Executors.newFixedThreadPool(4);

        for (int i = 0; i < 10; i++) {
            int id = i;
            executorService.submit(()->{
                try {
                    count.getCount();
                    System.out.println("任务开始 + " + id);
                    Thread.sleep(1000);
                    System.out.println("任务结束 + " + id);
                    count.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        //等待所有任务结束
        count.await();
        System.out.println("所有任务结束");
        executorService.shutdown();
    }
}
